package com.youkeyi.ddy.cloud.application.business.crm.order.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.BusinessCustomerVaccineOrderCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.dto.*;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.*;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.dto.BusinessVaccinumPlanListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.CrmCustomerCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.CrmCustomerVaccineOrderCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerVaccineOrderListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerVaccineOrderListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.CrmVaccineRecipientCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.dto.CrmVaccineRecipientListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.dto.CrmVaccineRecipientListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.CrmVaccinationDetailsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.dto.CrmVaccinationDetailsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.dto.CrmVaccinationDetailsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.StorageVaccinumCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.dto.StorageVaccinumListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.dto.StorageVaccinumListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * BusinessCustomerVaccineOrderController
 *
 * @Author liuxiawang
 * @Date 2024/4/3 10:28 AM
 * @Location shenzhen.china
 */
@Slf4j
@Service
public class BusinessCustomerVaccineOrderController implements BusinessCustomerVaccineOrderCloudService {

    @Autowired
    private CrmCustomerVaccineOrderCloudService crmCustomerVaccineOrderCloudService;
    @Autowired
    private CrmCustomerCloudService crmCustomerCloudService;
    @Autowired
    private CrmVaccineRecipientCloudService crmVaccineRecipientCloudService;
    @Autowired
    private CrmVaccinationDetailsCloudService crmVaccinationDetailsCloudService;
    @Autowired
    private StorageVaccinumCloudService storageVaccinumCloudService;

    @Override
    public BusinessCustomerVaccineOrderListCloudDto list(BusinessCustomerVaccineOrderListCloudParam param) {
        BusinessCustomerVaccineOrderListCloudDto dto = new BusinessCustomerVaccineOrderListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        // 客户关键字查询
        if (StringUtils.isNotBlank(param.getKeywords())) {
            CrmCustomerQueryCloudParam customerQueryCloudParam = new CrmCustomerQueryCloudParam();
            customerQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            customerQueryCloudParam.setKeywords(param.getKeywords());
            CrmCustomerListCloudDto customerDtos = crmCustomerCloudService.query(customerQueryCloudParam);
            if (null != customerDtos && CollectionUtils.isNotEmpty(customerDtos.getRows())) {
                if (CollectionUtils.isEmpty(param.getCustomerKeys())) {
                    param.getCustomerKeys().addAll(customerDtos.getRows().stream().map(CrmCustomerListItemCloudDto::getCustomerPrimaryKey).collect(Collectors.toList()));
                } else {
                    // 传入的客户keys不为空的话，需要取交集
                    param.getCustomerKeys().retainAll(customerDtos.getRows().stream().map(CrmCustomerListItemCloudDto::getCustomerPrimaryKey).collect(Collectors.toList()));
                }
            } else {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }
        }

        CrmCustomerVaccineOrderQueryCloudParam customerOrderQueryCloudParam = new CrmCustomerVaccineOrderQueryCloudParam();
        customerOrderQueryCloudParam.setCustomerVaccineOrderPrimaryKeys(param.getCustomerVaccineOrderPrimaryKeys());
        customerOrderQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        customerOrderQueryCloudParam.setCustomerKeys(param.getCustomerKeys());
        customerOrderQueryCloudParam.setCustomerNameFuzzy(param.getCustomerNameFuzzy());
        customerOrderQueryCloudParam.setGoodsNameFuzzy(param.getGoodsNameFuzzy());
        customerOrderQueryCloudParam.setGoodsCategoryKeys(param.getGoodsCategoryKeys());
        customerOrderQueryCloudParam.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrderQueryCloudParam.setSourceUserKey(param.getSourceUserKey());
        customerOrderQueryCloudParam.setServiceUserKey(param.getServiceUserKey());
        customerOrderQueryCloudParam.setCompletionTimeBegin(param.getCompletionTimeBegin());
        customerOrderQueryCloudParam.setCompletionTimeEnd(param.getCompletionTimeEnd());
        customerOrderQueryCloudParam.setRegistrationTimeBegin(param.getRegistrationTimeBegin());
        customerOrderQueryCloudParam.setRegistrationTimeEnd(param.getRegistrationTimeEnd());
        customerOrderQueryCloudParam.setPotentialDemandKey(param.getPotentialDemandKey());
        customerOrderQueryCloudParam.setStatuses(param.getStatuses());
        customerOrderQueryCloudParam.setPageSize(param.getPageSize());
        customerOrderQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerVaccineOrderListCloudDto customerOrderDtos = crmCustomerVaccineOrderCloudService.query(customerOrderQueryCloudParam);
        if (null != customerOrderDtos && CollectionUtils.isNotEmpty(customerOrderDtos.getRows())) {
            // 查询客户信息
            CrmCustomerQueryCloudParam customerQueryCloudParam = new CrmCustomerQueryCloudParam();
            customerQueryCloudParam.setCustomerPrimaryKeys(customerOrderDtos.getRows().stream().map(CrmCustomerVaccineOrderListItemCloudDto::getCustomerKey).collect(Collectors.toList()));
            customerQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            CrmCustomerListCloudDto customerDtos = crmCustomerCloudService.query(customerQueryCloudParam);
            Map<String, CrmCustomerListItemCloudDto> customerMap = new HashMap<>();
            if (null != customerDtos && CollectionUtils.isNotEmpty(customerDtos.getRows())) {
                customerMap.putAll(customerDtos.getRows().stream().collect(Collectors.toMap(CrmCustomerListItemCloudDto::getCustomerPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            List<BusinessCustomerVaccineOrderListItemCloudDto> items = new ArrayList<>();
            customerOrderDtos.getRows().forEach(customerOrder -> {
                BusinessCustomerVaccineOrderListItemCloudDto item = new BusinessCustomerVaccineOrderListItemCloudDto();
                item.setCustomerVaccineOrderPrimaryKey(customerOrder.getCustomerVaccineOrderPrimaryKey());
                item.setCustomerKey(customerOrder.getCustomerKey());
                item.setCustomerName(customerOrder.getCustomerName());
                if (customerMap.containsKey(customerOrder.getCustomerKey())) {
                    CrmCustomerListItemCloudDto customer = customerMap.get(customerOrder.getCustomerKey());
                    item.setCustomerWechatId(customer.getWechatId());
                    item.setCustomerPhone(customer.getPhone());
                }
                item.setGoodsKey(customerOrder.getGoodsKey());
                item.setGoodsName(customerOrder.getGoodsName());
                item.setGoodsCategoryKey(customerOrder.getGoodsCategoryKey());
                item.setGoodsCategoryName(customerOrder.getGoodsCategoryName());
                item.setServiceUserKey(customerOrder.getServiceUserKey());
                item.setServiceUserName(customerOrder.getServiceUserName());
                item.setRecipientKey(customerOrder.getRecipientKey());
                item.setRecipientName(customerOrder.getRecipientName());
                item.setRecipientSex(customerOrder.getRecipientSex());
                item.setRecipientBirthday(customerOrder.getRecipientBirthday());
                item.setRecipientIdCard(customerOrder.getRecipientIdCard());
                item.setCorpKey(customerOrder.getCorpKey());
                item.setIncomeRmb(customerOrder.getIncomeRmb());
                item.setIncomeHkb(customerOrder.getIncomeHkb());
                item.setAnnexKey(customerOrder.getAnnexKey());
                item.setSourceDepartmentKey(customerOrder.getSourceDepartmentKey());
                item.setSourceDepartmentName(customerOrder.getSourceDepartmentName());
                item.setSourceUserName(customerOrder.getSourceUserName());
                item.setSourceUserKey(customerOrder.getSourceUserKey());
                item.setCompletionTime(customerOrder.getCompletionTime());
                item.setRegistrationTime(customerOrder.getRegistrationTime());
                item.setPotentialDemandKey(customerOrder.getPotentialDemandKey());
                item.setPotentialDemandName(customerOrder.getPotentialDemandName());
                item.setRemark(customerOrder.getRemark());
                item.setStatus(customerOrder.getStatus());
                item.setStatusName(customerOrder.getStatusName());
                item.setFinanceCorpKey(customerOrder.getFinanceCorpKey());
                item.setConsultantWechat(customerOrder.getConsultantWechat());
                item.setVaccineDoses(customerOrder.getVaccineDoses());
                item.setGovernmentFundingFees(customerOrder.getGovernmentFundingFees());
                item.setCostPrice(customerOrder.getCostPrice());
                item.setHandlingFeeAmount(customerOrder.getHandlingFeeAmount());
                item.setPayTypeKey(customerOrder.getPayTypeKey());
                item.setPayTypeName(customerOrder.getPayTypeName());
                item.setPrePaymentDate(customerOrder.getPrePaymentDate());
                item.setLaboratoryKey(customerOrder.getLaboratoryKey());
                item.setLaboratoryName(customerOrder.getLaboratoryName());
                item.setDoctorKey(customerOrder.getDoctorKey());
                item.setDoctorName(customerOrder.getDoctorName());
                item.setSourceChannel(customerOrder.getSourceChannel());
                item.setIsDeleted(customerOrder.getIsDeleted());
                item.setIsDeletedName(customerOrder.getIsDeletedName());
                item.setCreateUserKey(customerOrder.getCreateUserKey());
                item.setCreateUserName(customerOrder.getCreateUserName());
                item.setCreateTime(customerOrder.getCreateTime());
                item.setUpdateUserKey(customerOrder.getUpdateUserKey());
                item.setUpdateUserName(customerOrder.getUpdateUserName());
                item.setUpdateTime(customerOrder.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(customerOrderDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCustomerVaccineOrderCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerVaccineOrderCreateCloudParam customerOrderCreateCloudParam = new CrmCustomerVaccineOrderCreateCloudParam();
        customerOrderCreateCloudParam.setCorpKey(corpKey);
        customerOrderCreateCloudParam.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrderCreateCloudParam.setCustomerKey(param.getCustomerKey());
        customerOrderCreateCloudParam.setCustomerName(param.getCustomerName());
        customerOrderCreateCloudParam.setGoodsKey(param.getGoodsKey());
        customerOrderCreateCloudParam.setGoodsName(param.getGoodsName());
        customerOrderCreateCloudParam.setGoodsCategoryKey(param.getGoodsCategoryKey());
        customerOrderCreateCloudParam.setGoodsCategoryName(param.getGoodsCategoryName());
        customerOrderCreateCloudParam.setServiceUserKey(param.getServiceUserKey());
        customerOrderCreateCloudParam.setServiceUserName(param.getServiceUserName());
        customerOrderCreateCloudParam.setRecipientKey(param.getRecipientKey());
        customerOrderCreateCloudParam.setRecipientName(param.getRecipientName());
        customerOrderCreateCloudParam.setRecipientSex(param.getRecipientSex());
        customerOrderCreateCloudParam.setRecipientBirthday(param.getRecipientBirthday());
        customerOrderCreateCloudParam.setRecipientIdCard(param.getRecipientIdCard());
        customerOrderCreateCloudParam.setIncomeRmb(param.getIncomeRmb());
        customerOrderCreateCloudParam.setIncomeHkb(param.getIncomeHkb());
        customerOrderCreateCloudParam.setAnnexKey(param.getAnnexKey());
        customerOrderCreateCloudParam.setRemark(param.getRemark());
        customerOrderCreateCloudParam.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrderCreateCloudParam.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrderCreateCloudParam.setSourceUserName(param.getSourceUserName());
        customerOrderCreateCloudParam.setSourceUserKey(param.getSourceUserKey());
        customerOrderCreateCloudParam.setCompletionTime(param.getCompletionTime());
        customerOrderCreateCloudParam.setRegistrationTime(param.getRegistrationTime());
        customerOrderCreateCloudParam.setPotentialDemandKey(param.getPotentialDemandKey());
        customerOrderCreateCloudParam.setPotentialDemandName(param.getPotentialDemandName());
        customerOrderCreateCloudParam.setStatus(param.getStatus());
        customerOrderCreateCloudParam.setConsultantWechat(param.getConsultantWechat());
        customerOrderCreateCloudParam.setVaccineDoses(param.getVaccineDoses());
        customerOrderCreateCloudParam.setGovernmentFundingFees(param.getGovernmentFundingFees());
        customerOrderCreateCloudParam.setCostPrice(param.getCostPrice());
        customerOrderCreateCloudParam.setHandlingFeeAmount(param.getHandlingFeeAmount());
        customerOrderCreateCloudParam.setPayTypeKey(param.getPayTypeKey());
        customerOrderCreateCloudParam.setPayTypeName(param.getPayTypeName());
        customerOrderCreateCloudParam.setPrePaymentDate(param.getPrePaymentDate());
        customerOrderCreateCloudParam.setLaboratoryKey(param.getLaboratoryKey());
        customerOrderCreateCloudParam.setLaboratoryName(param.getLaboratoryName());
        customerOrderCreateCloudParam.setDoctorKey(param.getDoctorKey());
        customerOrderCreateCloudParam.setDoctorName(param.getDoctorName());
        customerOrderCreateCloudParam.setSourceChannel(param.getSourceChannel());
        customerOrderCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerOrderCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerVaccineOrderCloudService.create(customerOrderCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessCustomerVaccineOrderUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerVaccineOrderPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerVaccineOrderUpdateCloudParam customerOrderUpdateCloudParam = new CrmCustomerVaccineOrderUpdateCloudParam();
        customerOrderUpdateCloudParam.setCustomerVaccineOrderPrimaryKey(param.getCustomerVaccineOrderPrimaryKey());
        customerOrderUpdateCloudParam.setCorpKey(corpKey);
        customerOrderUpdateCloudParam.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrderUpdateCloudParam.setCustomerKey(param.getCustomerKey());
        customerOrderUpdateCloudParam.setCustomerName(param.getCustomerName());
        customerOrderUpdateCloudParam.setGoodsKey(param.getGoodsKey());
        customerOrderUpdateCloudParam.setGoodsName(param.getGoodsName());
        customerOrderUpdateCloudParam.setGoodsCategoryKey(param.getGoodsCategoryKey());
        customerOrderUpdateCloudParam.setGoodsCategoryName(param.getGoodsCategoryName());
        customerOrderUpdateCloudParam.setServiceUserKey(param.getServiceUserKey());
        customerOrderUpdateCloudParam.setServiceUserName(param.getServiceUserName());
        customerOrderUpdateCloudParam.setRecipientKey(param.getRecipientKey());
        customerOrderUpdateCloudParam.setRecipientName(param.getRecipientName());
        customerOrderUpdateCloudParam.setRecipientSex(param.getRecipientSex());
        customerOrderUpdateCloudParam.setRecipientBirthday(param.getRecipientBirthday());
        customerOrderUpdateCloudParam.setRecipientIdCard(param.getRecipientIdCard());
        customerOrderUpdateCloudParam.setIncomeRmb(param.getIncomeRmb());
        customerOrderUpdateCloudParam.setIncomeHkb(param.getIncomeHkb());
        customerOrderUpdateCloudParam.setAnnexKey(param.getAnnexKey());
        customerOrderUpdateCloudParam.setRemark(param.getRemark());
        customerOrderUpdateCloudParam.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrderUpdateCloudParam.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrderUpdateCloudParam.setSourceUserName(param.getSourceUserName());
        customerOrderUpdateCloudParam.setSourceUserKey(param.getSourceUserKey());
        customerOrderUpdateCloudParam.setCompletionTime(param.getCompletionTime());
        customerOrderUpdateCloudParam.setRegistrationTime(param.getRegistrationTime());
        customerOrderUpdateCloudParam.setPotentialDemandKey(param.getPotentialDemandKey());
        customerOrderUpdateCloudParam.setPotentialDemandName(param.getPotentialDemandName());
        customerOrderUpdateCloudParam.setStatus(param.getStatus());
        customerOrderUpdateCloudParam.setConsultantWechat(param.getConsultantWechat());
        customerOrderUpdateCloudParam.setVaccineDoses(param.getVaccineDoses());
        customerOrderUpdateCloudParam.setGovernmentFundingFees(param.getGovernmentFundingFees());
        customerOrderUpdateCloudParam.setCostPrice(param.getCostPrice());
        customerOrderUpdateCloudParam.setHandlingFeeAmount(param.getHandlingFeeAmount());
        customerOrderUpdateCloudParam.setPayTypeKey(param.getPayTypeKey());
        customerOrderUpdateCloudParam.setPayTypeName(param.getPayTypeName());
        customerOrderUpdateCloudParam.setPrePaymentDate(param.getPrePaymentDate());
        customerOrderUpdateCloudParam.setLaboratoryKey(param.getLaboratoryKey());
        customerOrderUpdateCloudParam.setLaboratoryName(param.getLaboratoryName());
        customerOrderUpdateCloudParam.setDoctorKey(param.getDoctorKey());
        customerOrderUpdateCloudParam.setDoctorName(param.getDoctorName());
        customerOrderUpdateCloudParam.setSourceChannel(param.getSourceChannel());
        customerOrderUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerOrderUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerVaccineOrderCloudService.update(customerOrderUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCustomerVaccineOrderDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCustomerVaccineOrderPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerVaccineOrderDeleteCloudParam customerOrderDeleteCloudParam = new CrmCustomerVaccineOrderDeleteCloudParam();
        customerOrderDeleteCloudParam.setCustomerVaccineOrderPrimaryKeys(param.getCustomerVaccineOrderPrimaryKeys());
        customerOrderDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerOrderDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerVaccineOrderCloudService.delete(customerOrderDeleteCloudParam);
    }


    @Override
    public BusinessCustomerVaccineOrderRecipientDetailsCloudDto recipientDetails(BusinessCustomerVaccineOrderRecipientDetailsCloudParam param) {
        BusinessCustomerVaccineOrderRecipientDetailsCloudDto dto = new BusinessCustomerVaccineOrderRecipientDetailsCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param || StringUtils.isBlank(param.getRecipientIdCard())) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmVaccineRecipientQueryCloudParam vaccineRecipientQueryCloudParam = new CrmVaccineRecipientQueryCloudParam();
        vaccineRecipientQueryCloudParam.setIdCard(param.getRecipientIdCard());
        CrmVaccineRecipientListCloudDto vaccineRecipientDtos = crmVaccineRecipientCloudService.query(vaccineRecipientQueryCloudParam);
        if (null != vaccineRecipientDtos && CollectionUtils.isNotEmpty(vaccineRecipientDtos.getRows())) {
            CrmVaccineRecipientListItemCloudDto recipient = vaccineRecipientDtos.getRows().get(0);
            dto.setRecipientName(recipient.getName());
            dto.setRecipientSex(recipient.getSex());
            dto.setRecipientBirthday(recipient.getBirthday());
            dto.setRecipientIdCard(recipient.getIdCard());

            // 查询客户信息
            CrmCustomerQueryCloudParam customerQueryCloudParam = new CrmCustomerQueryCloudParam();
            customerQueryCloudParam.setCustomerPrimaryKeys(Collections.singletonList(recipient.getCustomerKey()));
            CrmCustomerListCloudDto customerDtos = crmCustomerCloudService.query(customerQueryCloudParam);
            if (null != customerDtos && CollectionUtils.isNotEmpty(customerDtos.getRows())) {
                CrmCustomerListItemCloudDto customer = customerDtos.getRows().get(0);
                dto.setCustomerPhone(customer.getPhone());
                dto.setCustomerEmail(customer.getEmail());
                dto.setCustomerWechatId(customer.getWechatId());
            }

            // 获取接种者订单信息
            CrmCustomerVaccineOrderQueryCloudParam customerOrderQueryCloudParam = new CrmCustomerVaccineOrderQueryCloudParam();
            customerOrderQueryCloudParam.setRecipientKeys(Collections.singletonList(recipient.getVaccineRecipientPrimaryKey()));
            CrmCustomerVaccineOrderListCloudDto customerOrderDtos = crmCustomerVaccineOrderCloudService.query(customerOrderQueryCloudParam);
            if (null != customerOrderDtos && CollectionUtils.isNotEmpty(customerOrderDtos.getRows())) {
                // 拿到订单key 获取订单明细
                List<String> orderKeys = customerOrderDtos.getRows().stream().map(CrmCustomerVaccineOrderListItemCloudDto::getCustomerVaccineOrderPrimaryKey).collect(Collectors.toList());
                List<String> orderGoodsKeys = customerOrderDtos.getRows().stream().map(CrmCustomerVaccineOrderListItemCloudDto::getGoodsKey).collect(Collectors.toList());



                CrmVaccinationDetailsQueryCloudParam vaccinationDetailsQueryCloudParam = new CrmVaccinationDetailsQueryCloudParam();
                vaccinationDetailsQueryCloudParam.setOrderKeys(orderKeys);
                // 查询订单明细
                CrmVaccinationDetailsListCloudDto vaccinationDetailsDtos = crmVaccinationDetailsCloudService.query(vaccinationDetailsQueryCloudParam);
                Map<String, List<CrmVaccinationDetailsListItemCloudDto>> orderDetailsMaps = new HashMap<>();
                if (null != vaccinationDetailsDtos && CollectionUtils.isNotEmpty(vaccinationDetailsDtos.getRows())) {
                    orderDetailsMaps.putAll(vaccinationDetailsDtos.getRows().stream().collect(Collectors.groupingBy(CrmVaccinationDetailsListItemCloudDto::getOrderKey)));
                }

                // 查询疫苗商品数据
                StorageVaccinumQueryCloudParam vaccinumQueryCloudParam = new StorageVaccinumQueryCloudParam();
                // vaccinumQueryCloudParam.setVaccinumPrimaryKeys(orderGoodsKeys); // 查询全部的疫苗商品数据
                StorageVaccinumListCloudDto vaccinumDtos = storageVaccinumCloudService.query(vaccinumQueryCloudParam);
                Map<String, StorageVaccinumListItemCloudDto> goodsMap = new HashMap<>();
                if (null != vaccinumDtos && CollectionUtils.isNotEmpty(vaccinumDtos.getRows())) {
                    goodsMap.putAll(vaccinumDtos.getRows().stream().collect(Collectors.toMap(StorageVaccinumListItemCloudDto::getVaccinumPrimaryKey, Function.identity(), (k1, k2) -> k1)));
                }


                // 计算已经打过的疫苗跟未打完的疫苗
                customerOrderDtos.getRows().forEach(customerOrder -> {
                    if (null != customerOrder.getVaccineDoses()) {
                        List<CrmVaccinationDetailsListItemCloudDto> orderDetails = orderDetailsMaps.get(customerOrder.getCustomerVaccineOrderPrimaryKey());
                        if (null == orderDetails) {
                            orderDetails = new ArrayList<>();
                        }
                        // 如果订单明细中有该订单数据，且条数对应的上的话，说明是已经全部打完的疫苗
                        if (orderDetailsMaps.containsKey(customerOrder.getCustomerVaccineOrderPrimaryKey()) && orderDetails.size() == customerOrder.getVaccineDoses()) {
                            BusinessCustomerVaccineOrderRecipientDetailsFinishedCloudDto finishedVaccine = new BusinessCustomerVaccineOrderRecipientDetailsFinishedCloudDto();
                            finishedVaccine.setVaccineName(customerOrder.getGoodsName());
                            finishedVaccine.setVaccineDoses(customerOrder.getVaccineDoses());
                            dto.getFinishedVaccine().add(finishedVaccine);
                        } else {
                            // 疫苗商品
                            StorageVaccinumListItemCloudDto vaccinumDto = goodsMap.get(customerOrder.getGoodsKey());

                            // 这里说明是未打完的疫苗
                            BusinessCustomerVaccineOrderRecipientDetailsUnfinishedCloudDto unfinishedVaccine = new BusinessCustomerVaccineOrderRecipientDetailsUnfinishedCloudDto();
                            unfinishedVaccine.setVaccineName(customerOrder.getGoodsName());
                            unfinishedVaccine.setVaccineDoses(customerOrder.getVaccineDoses());
                            unfinishedVaccine.setUnfinishedVaccineDoses(customerOrder.getVaccineDoses() - orderDetails.size());
                            unfinishedVaccine.getPlanItems().addAll(plan(vaccinumDto, dto.getRecipientBirthday(), orderDetails.size()));
                            dto.getUnfinishedVaccine().add(unfinishedVaccine);
                        }
                    } else {
                        log.error("订单{}中的疫苗计数未空，不参与计算", customerOrder.getCustomerVaccineOrderPrimaryKey());
                    }
                });


                // 计算推荐的疫苗
                if (null != vaccinumDtos && CollectionUtils.isNotEmpty(vaccinumDtos.getRows())) {
                    vaccinumDtos.getRows().forEach(vaccinumDto -> {
                        // 没有下过单的商品都推荐
                        if (!orderGoodsKeys.contains(vaccinumDto.getVaccinumPrimaryKey())) {
                            // 计算接种者到现在已经几个月了
                            // 当前时间 距离 出生日期，过了几个月了呢？
                            Integer periodsMonth = SystemDateUtils.getPeriodsMonth(dto.getRecipientBirthday(), System.currentTimeMillis());
                            // 对比疫苗的第一针是不是大于计算得到的月数
                            if (null != vaccinumDto.getPlanOne() && vaccinumDto.getPlanOne() > periodsMonth) {
                                BusinessCustomerVaccineOrderRecipientDetailsUnfinishedCloudDto unfinishedVaccine = new BusinessCustomerVaccineOrderRecipientDetailsUnfinishedCloudDto();
                                unfinishedVaccine.setVaccineName(vaccinumDto.getName());
                                unfinishedVaccine.setVaccineDoses(vaccinumDto.getVaccinationFrequency());
                                unfinishedVaccine.setUnfinishedVaccineDoses(vaccinumDto.getVaccinationFrequency());
                                unfinishedVaccine.getPlanItems().addAll(plan(vaccinumDto, dto.getRecipientBirthday(), 0));
                                dto.getRecommendedVaccine().add(unfinishedVaccine);
                            }
                        }
                    });
                }
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    /**
     * 疫苗计划计算
     */
    private List<BusinessVaccinumPlanListItemCloudDto> plan(StorageVaccinumListItemCloudDto vaccinumDto, Long recipientBirthday, int size) {
        List<BusinessVaccinumPlanListItemCloudDto> planItems = new ArrayList<>();
        Integer addMonthNumber = 0;
        String month = "%s个月";
        String remark = "第%s针";
        for (int i = 1; i <= vaccinumDto.getVaccinationFrequency(); i++) {
            // 第几针
            if (null != vaccinumDto.getVaccinationFrequency() && null != recipientBirthday) {
                if (i == 1 && size < i) {
                    if (null == vaccinumDto.getPlanOne()) {
                        break;
                    }
                    addMonthNumber = vaccinumDto.getPlanOne();
                    // 当前时间 距离 出生日期，过了几个月了呢？
                    Integer periodsMonth = SystemDateUtils.getPeriodsMonth(recipientBirthday, System.currentTimeMillis());

                    BusinessVaccinumPlanListItemCloudDto item = new BusinessVaccinumPlanListItemCloudDto();
                    item.setVaccinumName(vaccinumDto.getName());
                    item.setMonth(periodsMonth < 1 ? "出生" : String.format(month, periodsMonth));
                    item.setInoculabilityTime(SystemDateUtils.endTimestamp(recipientBirthday, vaccinumDto.getPlanOne()));
                    item.setRemark(String.format(remark, i));
                    planItems.add(item);
                }
                else if (i == 2 && size < i) {
                    if (null == vaccinumDto.getPlanTwo()) {
                        break;
                    }
                    addMonthNumber = addMonthNumber + vaccinumDto.getPlanTwo();

                    BusinessVaccinumPlanListItemCloudDto item = new BusinessVaccinumPlanListItemCloudDto();
                    item.setVaccinumName(vaccinumDto.getName());
                    item.setMonth(String.format(month, addMonthNumber));
                    item.setInoculabilityTime(SystemDateUtils.endTimestamp(recipientBirthday, addMonthNumber));
                    item.setRemark(String.format(remark, i));
                    planItems.add(item);
                }
                else if (i == 3 && size < i) {
                    if (null == vaccinumDto.getPlanThree()) {
                        break;
                    }
                    addMonthNumber = addMonthNumber + vaccinumDto.getPlanThree();

                    BusinessVaccinumPlanListItemCloudDto item = new BusinessVaccinumPlanListItemCloudDto();
                    item.setVaccinumName(vaccinumDto.getName());
                    item.setMonth(String.format(month, addMonthNumber));
                    item.setInoculabilityTime(SystemDateUtils.endTimestamp(recipientBirthday, addMonthNumber));
                    item.setRemark(String.format(remark, i));
                    planItems.add(item);
                }
                else if (i == 4 && size < i) {
                    if (null == vaccinumDto.getPlanFour()) {
                        break;
                    }
                    addMonthNumber = addMonthNumber + vaccinumDto.getPlanFour();

                    BusinessVaccinumPlanListItemCloudDto item = new BusinessVaccinumPlanListItemCloudDto();
                    item.setVaccinumName(vaccinumDto.getName());
                    item.setMonth(String.format(month, addMonthNumber));
                    item.setInoculabilityTime(SystemDateUtils.endTimestamp(recipientBirthday, addMonthNumber));
                    item.setRemark(String.format(remark, i));
                    planItems.add(item);
                }
                else if (i == 5 && size < i) {
                    if (null == vaccinumDto.getPlanFive()) {
                        break;
                    }
                    addMonthNumber = addMonthNumber + vaccinumDto.getPlanFive();

                    BusinessVaccinumPlanListItemCloudDto item = new BusinessVaccinumPlanListItemCloudDto();
                    item.setVaccinumName(vaccinumDto.getName());
                    item.setMonth(String.format(month, addMonthNumber));
                    item.setInoculabilityTime(SystemDateUtils.endTimestamp(recipientBirthday, addMonthNumber));
                    item.setRemark(String.format(remark, i));
                    planItems.add(item);
                }
            } else {
                log.error("疫苗商品{}中计次为空，不参与计算", vaccinumDto.getVaccinumPrimaryKey());
            }
        }
        return planItems;
    }
}
